import Vue from 'vue'
import VueRouter from 'vue-router'
// import store from '@/store'
import MyLogin from '../views/MyLogin.vue'
// 嵌套路由
// import oneNested from '@/views/nested/OneNested'
// layout
import layout from '@/layout/MyLayOut'
import hm from '@/views/hm/HmIndex.vue'
import home from '@/views/home/MyHome.vue'
import operation from '@/views/workOrder/MyOperation'
import business from '@/views/workOrder/MyBusiness'
import { getToken } from '@/utils/storage'
// import two from '@/components/TwoRouter.vue'

// 点位
import PointDistrict from '@/views/Point/PointDistrict'
import MyPoint from '@/views/Point/MyPoint'
import PointCooperation from '@/views/Point/PointCooperation'
// 设备
import EqManage from '@/views/equipment/EqManagement.vue'
import EqStatus from '@/views/equipment/EqStatus'
import EqType from '@/views/equipment/EqType'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    // name: 'home',
    component: MyLogin
  },
  {
    path: '/',
    component: layout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: home
      },

      {
        path: '/task/business',
        component: business

      },

      {
        path: '/task/operation',
        component: operation
      },
      {
        path: '/node/region',
        component: PointDistrict
      },
      {
        path: '/node/node',
        component: MyPoint
        // /node/partner
      },
      {
        path: '/node/partner',
        component: PointCooperation
        // /node/partner
      },
      {
        path: '/vm/index',
        component: EqManage
      },
      {
        path: '/vm/status',
        component: EqStatus
      },
      {
        path: '/vm/type',
        component: EqType
      }
    ]
  },

  {
    path: '/itheima',
    component: hm
  }
]

const router = new VueRouter({
  routes
})
const whiteList = ['/login']

router.beforeEach((to, from, next) => {
  // const userInfo = store.state.user.userInfo
  const token = JSON.parse(getToken())
  if (!token) {
    // console.log('没有userId,去白名单')
    if (whiteList.includes(to.path)) {
      console.log('没有userInfo,去白名单')
      next()
    } else {
      console.log('没有userInfo不去白名单')
      next('/login')
    }
  } else {
    if (to.path === '/login') {
      console.log('有userInfo,去login,不给跳转')
      next('/layout')
    } else {
      console.log('不是去login,随便跳转')
      next()
    }
  }
})
export default router
